package com.neusoft.neuiotms.common.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.dto.SysTaskDTO;
import com.neusoft.neuiotms.common.model.SysTask;
import com.neusoft.neuiotms.common.model.enums.SysTaskStatus;
import com.neusoft.neuiotms.common.repository.SysTaskRepository;

@Component
public class SysTaskConverter extends AbstractConverter<SysTask, SysTaskDTO> {

    @Autowired
    private SysTaskRepository sysTaskRepository;

    @Override
    public SysTask toModel(final SysTaskDTO dto) {
        SysTask model;
        if (dto.isNew()) {
            model = new SysTask();
        } else {
            model = this.sysTaskRepository.getOne(dto.getId());
        }
        model.setName(dto.getName());
        model.setCode(dto.getCode());
        model.setCron(dto.getCron());
        model.setStatus(SysTaskStatus.valueOf(dto.getStatus()));
        model.setJobClass(dto.getJobClass());
        model.setJobGroup(dto.getJobGroup());
        model.setJobDesc(dto.getJobDesc());
        return model;
    }

    @Override
    public SysTaskDTO toDTO(final SysTask model, final boolean forListView) {

        final SysTaskDTO dto = new SysTaskDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setCode(model.getCode());
        dto.setCron(model.getCron());
        dto.setStatus(model.getStatus().toString());
        dto.setJobClass(model.getJobClass());
        dto.setJobGroup(model.getJobGroup());
        dto.setJobDesc(model.getJobDesc());
        return dto;
    }

}
